package cn.itcast.bos.base.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.base.service.AreaService;
import cn.itcast.bos.domain.base.Area;

import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class AreaAction extends BaseAction<Area>{
	private File file;
	
	@Autowired
	private AreaService areaservice;
	public void setFile(File file) {
		this.file = file;
	}
	
	@Action(value="uploadExcel")
	public String uploadExcel() throws Exception{
		List<Area> list=new ArrayList<Area>();
		Workbook wb=new XSSFWorkbook(file);
		Sheet sheet = wb.getSheetAt(0);
		for (Row row : sheet) {
			if(row.getRowNum()==0){
				continue;
			}
			if(row.getCell(0)==null|| StringUtils.isBlank(row.getCell(0).getStringCellValue())){
				continue;
			}
			Area area=new Area();
			area.setId(row.getCell(0).getStringCellValue());
			String proviceStr=row.getCell(1).getStringCellValue();
			area.setProvince(proviceStr);
			String cityStr=row.getCell(2).getStringCellValue();
			area.setCity(cityStr);
			String districtStr=row.getCell(3).getStringCellValue();
			area.setDistrict(districtStr);
			area.setPostcode(row.getCell(4).getStringCellValue());
			area.setCitycode(PinyinHelper.convertToPinyinString(cityStr.substring(0, cityStr.length()-1),"", PinyinFormat.WITHOUT_TONE));
			StringBuilder builder=new StringBuilder();
			String shortcode = builder.append(proviceStr.substring(0, proviceStr.
					length()-1)).append(cityStr.subSequence(0, cityStr.length()-1)).
					append(districtStr.substring(0, districtStr.length()-1)).toString();
			area.setShortcode(PinyinHelper.getShortPinyin(shortcode));
			list.add(area);
		}
		areaservice.setAreas(list);
		return NONE;
	}
	
	@Action(value="searchArea",results={@Result(name="success",type="json")})
	public String searchArea(){
		Specification<Area> spec=new Specification<Area>() {
			
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list=new ArrayList<Predicate>();
//				province:
				if(StringUtils.isNotBlank(t.getProvince())){
					Predicate p1 = cb.like(root.get("province").as(String.class), "%"+t.getProvince()+"%");
					list.add(p1);
				}
//				city:
				if(StringUtils.isNotBlank(t.getCity())){
					Predicate p2 = cb.like(root.get("city").as(String.class), "%"+t.getCity()+"%");
					list.add(p2);
				}
//				district:
				if(StringUtils.isNotBlank(t.getDistrict())){
					Predicate p3 = cb.like(root.get("district").as(String.class), "%"+t.getDistrict()+"%");
					list.add(p3);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Pageable pageable=new PageRequest(page-1, rows);
		Page<Area> searchArea = areaservice.searchArea(spec, pageable);
		putIntoValueStack(searchArea);
		return SUCCESS;
	}

}
